<script setup>
import { ref, computed } from 'vue'
import {getFloor} from "@/api/system/floor.js";
import {listDorm} from "@/api/system/dorm.js";
import {useRoute, useRouter} from "vue-router";

const route = useRoute()
const router = useRouter()

const queryParams = reactive({})
const floorId = route.query.floorId // 输出"vue3"


const floorData = ref({})
const dorms = ref([])

// 转换寝室数据为 Map 方便查询
const dormMap = computed(() => {
  const map = new Map()
  dorms.value.forEach(dorm => {
    const key = `${dorm.xIndex}-${dorm.yIndex}`
    map.set(key, dorm)
  })
  return map
})

// 根据坐标获取寝室名称
const getDormName = (x, y) => {
  return dormMap.value.get(`${x}-${y}`)?.dormName
}

// 根据坐标获取寝室ID
const getDormId = (x, y) => {
  return dormMap.value.get(`${x}-${y}`)?.dormId
}

// 点击事件处理
const handleDormClick = (dormId) => {
  if(dormId) {
    console.log('选中寝室ID:', dormId)
    let path = "/dorm?dormId=" + dormId
    router.push(path)
  }
}

// 滚动事件（可按需扩展）
const handleScroll = (e) => {
  // 可添加滚动位置记录等逻辑
}

function getList() {
  getFloor(floorId).then(response => {
    floorData.value = response.data;
  });
  queryParams.floorId = floorId
  queryParams.pageSize = 1000
  listDorm(queryParams).then(response => {
    dorms.value = response.rows;
    console.log(dorms.value)
  });
}
getList();

</script>

<template>
  <div class="floor-container">
    <div class="floor-header">
      <h2>{{ floorData.floorName }}</h2>
    </div>

    <div class="floor-body">
      <!-- 左侧宿舍列 -->
      <div class="dorm-column left-column">
        <div
            v-for="y in floorData.leftDormCounts"
            :key="'left-'+y"
            class="dorm-cell"
            @click="handleDormClick(getDormId(0, y-1))">
          {{ getDormName(0, y-1) || `左${y}` }}
        </div>
      </div>

      <!-- 双竖线走廊 -->
      <div class="corridor">
        <div class="corridor-line left-line"></div>
        <div class="corridor-text">走 廊</div>
        <div class="corridor-line right-line"></div>
      </div>

      <!-- 右侧宿舍列 -->
      <div class="dorm-column right-column">
        <div
            v-for="y in floorData.rightDormCounts"
            :key="'right-'+y"
            class="dorm-cell"
            @click="handleDormClick(getDormId(1, y-1))">
          {{ getDormName(1, y-1) || `右${y}` }}
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.floor-container {
  height: 94vh;
  display: flex;
  flex-direction: column;
  background: #f8f9fa;
}

.floor-header {
  padding: 12px;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  font-size: 14px;
  font-weight: 500; /* 中等字重 */
  text-shadow: 0 1px 1px rgba(0,0,0,0.1); /* 轻微阴影提升对比度 */
  color: #333;
}

.floor-body {
  flex: 1;
  display: flex;
  overflow-y: auto;
  padding: 0 8px;
}

.dorm-column {
  flex: 1;
  min-width: 30%;
  padding: 8px 0;
}

.corridor {
  width: 40px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.corridor-line {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 1px;
  background: #999;
}

.left-line {
  left: 0;
}

.right-line {
  right: 0;
}

.corridor-text {
  color: #666;
  writing-mode: vertical-lr;
  text-align: center;
  letter-spacing: 4px;
  font-size: 16px;
}

.dorm-cell {
  height: 60px;
  margin: 6px;
  background: #ffffff;
  border: 1px solid #e0e0e0;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 500; /* 中等字重 */
  text-shadow: 0 1px 1px rgba(0,0,0,0.1); /* 轻微阴影提升对比度 */
  color: #333;
  transition: all 0.2s;
}

.dorm-cell:hover {
  transform: translateY(-2px);
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
</style>